# coding=utf-8
from flask import Flask
from flask import request
from flask import jsonify
import sql_link
import compute_score

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def index():
    print('请求方式为------->', request.method)
    args = request.args.get("name") or "args没有参数"
    print('args参数是------->', args)
    form = request.form.get('name') or 'form 没有参数'
    print('form参数是------->', form)
    json = request.get_json().get('name') or 'json 没有参数'
    print('json参数是------->', json)
    return jsonify(args=args, form=form, json=json)

# 获取token
@app.route('/token', methods=['POST'])
def get_token():
    params = request.get_json()
    print(params.get('client_id'))
    print(params.get('client_secret'))
    return jsonify(ret_code=1, ret_msg='success', expires_in = 2592000, access_token = 'fdsfsdf5445fdsf')

# 获取评分
@app.route('/getCredit', methods=['POST'])
def get_credit():
    # 获取请求参数
    params=request.get_json()
    # 打印参数值到请求端
    print("params: ")
    for item in params.items():
        print(item)
    # 将请求数据插入数据库
    sql_link.insert_into_database(params)
    # 计算评分操作
    print('*******************ready to compute credit scores**************************')
    scget=compute_score.compute_credit_score(params)
    pd=scget.get('pd')
    score=scget.get('score')
    print('*******************credit score successfully computed**************************')
    # 将评分结果插入数据库并
    sc2db=sql_link.score_to_database(params, pd.value[0], score)
    # 打印结果给请求端
    return jsonify(sc2db)

if __name__ == '__main__':
    app.run(host='0.0.0.0',port= 5100)